<template>
  <div class="ImageUpload">
    <!-- 图片上传组件 -->
    <el-upload
      v-if="!imageUrl"
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <!-- 如果有图片则显示默认图片 -->
    <div v-else class="mybox">
      <img :src="imageUrl" alt="" class="avatar" @click="openShow" v-imgerror='defaultImg'/>
      <!-- 右上角关闭按钮 -->
      <i @click="clearImg" class="el-icon-close mycross"></i>
      </div>
      <el-dialog title="预览图片" :visible.sync="show" class="mydialog">
        <img :src="imageUrl" />
      </el-dialog>
      <!-- 进度条 -->
      <el-progress
      v-if="showProgress"
      class="myprogress"
      :show-text="false"
      :percentage="percentage"
      ></el-progress>
  </div>
  
</template>

<script>
import COS from 'cos-js-sdk-v5'
export default {
  name: 'ImageUpload',
  props: ['imageUrl'],
  data () {
    return {
      show: false,
      cosObj:null,
      //控制进度条显示与隐藏
      showProgress:false,
      //进度条的显示
      percentage:0,
      //默认图片
      defaultImg:require('@/assets/common/head.jpg')
    }
  },
  methods: {
    //上传之前会触发的回调函数
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 或 png 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    openShow () {
      this.show = true
    },
    //删除图片
    clearImg(){
      this.$emit('update:imageUrl','')
    },
     httpRequest (content) {
      // 得到上传的文件
      this.showProgress = true
      const file = content.file
      this.cosObj.putObject(
        {
          Bucket: 'aihaikuan-1258099533' /* 必须 */,
          Region: 'ap-chongqing' /* 存储桶所在地域，必须字段 */,
          Key: file.name /* 必须 */,
          StorageClass: 'STANDARD',
          Body: file, // 上传文件对象
          onProgress: progressData => {
            // 上传的进度
            // progressData: 上传过程中的实时进度对象
            // console.log(JSON.stringify(progressData))
            this.percentage = progressData.percent * 100
          }
        },
        (err, data) => {
          // 上传之后的回调函数
          // data: 上传成功后的返回值
          // err: 上传出错的错误信息
          // 将返回的图片地址保存起来： imageUrl
          console.log(data);
          this.showProgress = false
          this.$emit('update:imageUrl', `https://${data.Location}`)
        }
      )
    },
  },
  created(){
    this.cosObj = new COS({
      SecretId: 'AKIDfCVZOyLUN1SgeR0OZ8VkoeFcmjQs67uU',
      SecretKey: 'QdcMsAGArMehV6TedPwGMDrMehZDeqnW'
    })
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.mydialog {
  text-align: center;
  img {
    width: 300px;
  }
}
.mybox{
  position: relative;
  display: inline-block;
  .mycross{
    position: absolute;
    right: 0;
    top: 0;
    background-color: rgba(0,0,0,0.4);
    color: #fff;
    &:hover{
      cursor: pointer;
    }
  }
}
</style>
